package compilador;

import java.io.IOException;
import java.util.Properties;

/**
 * @author danilo
 * 
 */
public class CommandBuilder {

	private final static Properties props = new Properties();

	static {
		try {
			props.load(ClassLoader
					.getSystemResourceAsStream("commands.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public static Command createCommand(String cmdName) throws InvalidCommandException {
		try {
			Class klass = Class.forName(props.getProperty(cmdName));
			Command cmd = (Command) klass.newInstance();
			return cmd;
		} catch (ClassNotFoundException e) {
			throw new InvalidCommandException("Comando " + cmdName + " não existe!");
		} catch (InstantiationException e) {
			throw new InvalidCommandException("Comando " + cmdName + " não possui construtor vazio!");
		} catch (IllegalAccessException e) {
			throw new InvalidCommandException("Comando " + cmdName + " não possui construtor publico!");
		}
	}

}
